package com.rjwh.dingdong.client.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.localbean.ScoreRankListItemData;
import com.rjwh.dingdong.client.util.ViewHolder;

/**
 * @author 王小珂
 * @ClassName HomeWorkRankListAdapter
 * @Description
 * @date 2017-1-4
 * @history 2017-1-4 author: description:
 */

public class HomeWorkRankListAdapter extends AdapterBase<ScoreRankListItemData> {
    private Context context;

    public HomeWorkRankListAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected View getExView(int position, View convertView, ViewGroup parent) throws Exception {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.home_work_rank_list_item, null);
        }

        ImageView homeWorkRankListItemLevelImg = ViewHolder.get(convertView, R.id.home_work_rank_list_item_level_img);
        TextView homeWorkRankListItemLevelTv = ViewHolder.get(convertView, R.id.home_work_rank_list_item_level_tv);
        TextView homeWorkRankListItemNameTv = ViewHolder.get(convertView, R.id.home_work_rank_list_item_name_tv);
        TextView homeWorkRankListItemTelTv = ViewHolder.get(convertView, R.id.home_work_rank_list_item_tel_tv);
        TextView homeWorkRankListItemPointsTv = ViewHolder.get(convertView, R.id.home_work_rank_list_item_points_tv);

        ScoreRankListItemData scoreRankListItemDate = getList().get(position);
        if (scoreRankListItemDate == null) {
            return convertView;
        }

        String rankNO = scoreRankListItemDate.getRankNO();
        String name = scoreRankListItemDate.getName();
        String score = scoreRankListItemDate.getScore();

        if (!AsStrUtils.isEmpty(name)) {
            homeWorkRankListItemNameTv.setText(name);
        }
        if (!AsStrUtils.isEmpty(score)) {
            homeWorkRankListItemPointsTv.setText(score);
        }

        if (position > 2) {
            homeWorkRankListItemLevelTv.setVisibility(View.VISIBLE);
            homeWorkRankListItemLevelImg.setVisibility(View.INVISIBLE);
            if (!AsStrUtils.isEmpty(rankNO)) {
                homeWorkRankListItemLevelTv.setText(rankNO);
            } else {
                homeWorkRankListItemLevelTv.setText(String.valueOf(position + 1));
            }
        } else {
            homeWorkRankListItemLevelImg.setVisibility(View.VISIBLE);
            homeWorkRankListItemLevelTv.setVisibility(View.INVISIBLE);

            switch (position) {
                case 0:
                    homeWorkRankListItemLevelImg.setImageResource(R.drawable.icon_rank_1);
                    break;
                case 1:
                    homeWorkRankListItemLevelImg.setImageResource(R.drawable.icon_rank_2);
                    break;
                case 2:
                    homeWorkRankListItemLevelImg.setImageResource(R.drawable.icon_rank_3);
                    break;
                default:
                    break;
            }
        }

        return convertView;
    }
}
